package dev.polls.skycube;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import dev.polls.skycube.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HomeActivity extends Activity {
	TextView text;
	LinearLayout linear;
	User userclass = new User();
	int userId = 1;
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		
        
        linear = (LinearLayout)findViewById(R.id.linearLayout2);
        
        final TextView c = new TextView(this);
        final TextView userName = (TextView)findViewById(R.id.userName);
        final TextView companyName = (TextView)findViewById(R.id.companyName);
        final TextView jobName = (TextView)findViewById(R.id.jobName);
        final TextView tv = (TextView)findViewById(R.id.personInfo);
        final TextView updateTitle = (TextView)findViewById(R.id.updateTitle);
        updateTitle.setBackgroundResource(R.drawable.bg_butt_h);
        tv.setBackgroundResource(R.drawable.bg_butt_h);

        try {
			JSONObject jso = getJson();
			
			String title = jso.getString("Title");
			String firstname = jso.getString("Firstname");
			String lastname = jso.getString("Lastname");
			String jobtitle = jso.getString("JobTitle");
			userName.setText(title+" "+firstname+" "+lastname);
			jobName.setText(jobtitle);
			companyName.setText(getCompany());
			
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
        c.setTextAppearance(this, android.R.style.TextAppearance_Large);
        c.setBackgroundResource(R.drawable.bg_butt_h);
        c.setText("Show Personal Information");
        c.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                linear.removeView(c);
                
                View pl = (View)findViewById(R.id.personal_layout);
				Dropdown transform = new Dropdown(pl, 500);
				pl.startAnimation(transform);
				linear.addView(tv);
            }
        });
        tv.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                linear.removeView(tv);
                                              
                View pl = (View)findViewById(R.id.personal_layout);
				Dropdown transform = new Dropdown(pl, 500);
				pl.startAnimation(transform);
				linear.addView(c);
            }
        });
	}
	
	public JSONObject getJson() throws ClientProtocolException, URISyntaxException, IOException, JSONException
	{
		GetJSon js = new GetJSon();
		SharedPreferences settings = getSharedPreferences("account_info",0);
		String us_name = settings.getString("username", "");
		String pass_word = settings.getString("password", "");
		//System.out.println(us_name+" "+pass_word);
		JSONObject jo = js.Json_Values(us_name,pass_word); 
		return jo;
	}
	public String getCompany() throws ClientProtocolException, URISyntaxException, IOException, JSONException
	{
		
		String name = null;
		GetJSon js = new GetJSon();
		SharedPreferences settings = getSharedPreferences("account_info",0);
		String us_name = settings.getString("username", "");
		String pass_word = settings.getString("password", "");
		JSONArray ja = js.getCompany(us_name, pass_word);
		for (int i = 0; i < ja.length(); ++i) {
            JSONObject rec = ja.getJSONObject(i);
            name = rec.getString("Name_n");
            
         }
		return name;
	}
}
	
